# model 

# perform the delete-process of a chosen key (raid-1)

from google.appengine.ext import webapp

from controller.BuildStorageInfo import *

from Utilities import *


class DeleteBucketKeyRaidOne(webapp.RequestHandler):
    def get(self, pSelf, md5):
        utilities = Utilities()
        
        # get some necessary attributes
        userText = utilities.getUserText()
        username = utilities.getUsername()

        # get the storage-names
        amazon = utilities.getAmazonText()
        google = utilities.getGoogleText()
        openStack = utilities.getOpenStackText()
        
        if username:
            urlText = utilities.getUrlTextLogout()
            url = utilities.getLogout(pSelf)
            
            # determine the given credentials
            credentials = utilities.getCredentials(username)
            results = credentials.fetch(100)
            
            # initialize the storage-variables
            amazonStorageConnection = None
            googleStorageConnection = None
            openStackStorageConnection = None
            
            # determine the logged in storage-services
            for result in results:
                storageService = result.credentialType
                
                if storageService == amazon:
                    amazonStorageService = storageService
                    amazonStorageConnection, regionname = utilities.getStorageServiceConnection(username, amazonStorageService)
                elif storageService == google:
                    googleStorageService = storageService
                    googleStorageConnection, regionname = utilities.getStorageServiceConnection(username, googleStorageService)
                elif storageService == openStack:
                    openStackStorageService = storageService
                    openStackStorageConnection, regionname = utilities.getStorageServiceConnection(username, openStackStorageService)

            # determine the bucketname
            bucketname = utilities.getBucketname(username)
                    
            # initialize the key-lists for the available storages
            keyListAmazon = []
            keyListGoogle = []
            keyListOpenStack = [] 
            
            # get the keys from the specific storage-services
            # check if amazonStorageConnection is available
            if amazonStorageConnection:
                keyListAmazon = utilities.getBucketKeyList(amazonStorageConnection, bucketname)
            
            # check if googleStorageConnection is available
            if googleStorageConnection:
                keyListGoogle = utilities.getBucketKeyList(googleStorageConnection, bucketname)
                
            # check if openStackStorageConnection is available
            if openStackStorageConnection:
                keyListOpenStack = utilities.getBucketKeyList(openStackStorageConnection, bucketname)

            # get the bucket-entry (key) with the chosen md5-checksum, which the user would like to delete
            keyToDeleteAmazon = utilities.getBucketKeyByChecksum(username, keyListAmazon, md5)
            keyToDeleteGoogle = utilities.getBucketKeyByChecksum(username, keyListGoogle, md5)
            
            # original way to get the bucket-keys
            # DO NOT DELETE!
            #keyToDeleteOpenStack = utilities.getBucketKeyByChecksum(username, keyListOpenStack, md5)
            
            # get some necessary attributes..
            keyname = self.getKeynameForOpenStackKeyByMD5(utilities, md5)
            fileSize = self.getFileSizeForOpenStackKeyByMD5(utilities, md5)
            
            keyToDeleteOpenStack = utilities.getBucketKeyByKeynameAndFileSize(keyname, fileSize, keyListOpenStack)
            # workaround to get the bucket-keys by the md5-checksum (from the local database)
#            keyToDeleteOpenStackLocal = utilities.getKeyByMD5ForOpenstack(md5)

            # check if the right key to delete was found - when not, print a hint-text
            if keyToDeleteAmazon == None or keyToDeleteGoogle == None or keyToDeleteOpenStack == None:
#                hintText = _('An error occurred - the chosen key to delete could not be found')
                hintText = 'An error occurred - the chosen key to delete could not be found'
                hintColor = 'red'
                
                self.callBuilder(pSelf, userText, username, url, urlText, hintText, hintColor, keyListAmazon)
            
            # delete the entry (key)
            # the builder-class needs a list of keys to show them on the screen - default is the amazon-key-list (could be anyone else..)
            self.deleteKey(pSelf, utilities, amazon, keyToDeleteAmazon, userText, username, url, urlText, keyListAmazon)
            self.deleteKey(pSelf, utilities, google, keyToDeleteGoogle, userText, username, url, urlText, keyListAmazon)
            self.deleteKey(pSelf, utilities, openStack, keyToDeleteOpenStack, userText, username, url, urlText, keyListAmazon, keyname)
        else:
            pSelf.redirect('/')
    
    # responsible for the delete-process...
    def deleteKey(self, pSelf, utilities, regionname, keyToDelete, userText, username, url, urlText, keyListAmazon, keyname=None):
        if keyToDelete != None:
            result = utilities.deleteBucketKey(regionname, keyToDelete, keyname)
            
            if result != None:
#                hintText = _('The key was deleted successfully')
                hintText = 'The key was deleted successfully'
                hintColor = 'green'
                
                self.callBuilder(pSelf, userText, username, url, urlText, hintText, hintColor, keyListAmazon)
                pSelf.redirect('/selectdata')
            elif result == None:
#                hintText = _('An error occurred - key could not be deleted')
                hintText = 'An error occurred - key could not be deleted'
                hintColor = 'red'
                
                self.callBuilder(pSelf, userText, username, url, urlText, hintText, hintColor, keyListAmazon)
                pSelf.redirect('/selectdata')
                
        elif keyToDelete == None:
#            hintText = _('An error occurred - unknown key')
            hintText = 'An error occurred - unknown key'
            hintColor = 'red'
            
            self.callBuilder(pSelf, userText, username, url, urlText, hintText, hintColor, keyListAmazon)
            pSelf.redirect('/selectdata')
            
    # search the given key-list for an entry by it's md5-value and return the key-name
    def getKeynameForOpenStackKeyByMD5(self, utilities, md5):
        keyname = utilities.getKeynameForOpenStackKeyByMD5(md5)
        return keyname
    
    # search the given key-list for an entry by it's md5-value and return the file-size
    def getFileSizeForOpenStackKeyByMD5(self, utilities, md5):
        fileSize = utilities.getFileSizeForOpenStackKeyByMD5(md5)
        return fileSize

    # responsible for the call of the get-method of the BuildStorageInfo-Class
    def callBuilder(self, pSelf, userText, username, url, urlText, hintText, hintColor, keyList):
        templateValues = {
                          'userText': userText,
                          'user': username,
                          'urlText': urlText,
                          'url': url,
                          'urlText': urlText,
                          'hintText': hintText,
                          'hintColor': hintColor
                          }
        
        builder = BuildStorageInfo()
        builder.get(pSelf, templateValues, keyList)            
            

